import requests
import psycopg2
import pandas
config_postgresql = {'host': 'matrix', 'database': 'origin', 'port': 5432, 'user': 'mxadmin', 'password': '123123'}

conn = psycopg2.connect(database=config_postgresql['database'],
                        user=config_postgresql['user'],
                        password=config_postgresql['password'],
                        host=config_postgresql['host'],
                        port=config_postgresql['port'])

task_id = 1
data_id = 9
cols = ['id', 'b', 'c']
# 如有中文/大写列名，需要在列名加双引号，如sql = f"select \"{cols[0]}\",\"{cols[1]}\",\"{cols[2]}\" from task_{task_id}.data_{data_id} order by id"
sql = f"select {cols[0]},{cols[1]},{cols[2]} from task_{task_id}.data_{data_id} order by id"
cursor = conn.cursor()
cursor.execute(sql)

# 转换为pandas.DataFrame
df = pandas.DataFrame(cursor.fetchall(), columns=cols)
print(df)

